﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinCourseControls
{
    public partial class FrmPanel : Form
    {
        public FrmPanel()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 确定按钮-获取选中控件信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            List<string> chks = new List<string>();
            string rbtn = "";
            foreach (Control c in panelContents.Controls)
            {
                if(c is CheckBox)
                {
                    CheckBox chk = c as CheckBox;
                    if (chk.Checked)
                        chks.Add(chk.Text);
                }
                else if(c is RadioButton)
                {
                    RadioButton btn = c as RadioButton;
                    if (btn.Checked)
                        rbtn = btn.Text;
                }
            }
            string str = $"选择的选项有：{string.Join(",", chks)}; 单选是：{rbtn}";
            lblResult.Text = str;
        }

        /// <summary>
        /// 加载事件-动态加载控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmPanel_Load(object sender, EventArgs e)
        {
            panelContents.Controls.Clear();
            for (int i = 1; i <= 7; i++)
            {
                CheckBox chk = new CheckBox();
                chk.Text = "选项" + i.ToString("00");
                chk.Size = new Size(60, 25);  // 控件大小
                chk.Location = new Point(20 + 70 * (i - 1), 10);  // 控件位置
                chk.CheckAlign = ContentAlignment.TopLeft;  // 对齐方式
                panelContents.Controls.Add(chk);
            }
            for (int i = 1; i <= 7; i++)
            {
                RadioButton  rbtn = new RadioButton();
               rbtn.Text = "单选 " + i.ToString("00");
               rbtn.Size = new Size(60, 25);
                rbtn.Location = new Point(20 + 70 * (i - 1), 50);
                rbtn.CheckAlign = ContentAlignment.TopLeft;
                panelContents.Controls.Add(rbtn);
            }
        }
    }
}
